package com.scm.servlet.pomain;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.scm.dao.PoitemDao;
import com.scm.dao.PomainDao;
import com.scm.dao.ProductDao;
import com.scm.model.Poitem;
import com.scm.model.Pomain;
import com.scm.util.DbUtil;

/**
 * Servlet implementation class ChangePomainServlet
 */
@WebServlet("/main/pomain/ChangePomain")
public class ChangePomainServlet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}


	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String poId = req.getParameter("poId");
		String createTime = req.getParameter("createTime");
		String venderCode = req.getParameter("venderCode");
		String account = req.getParameter("account");
		String tipFee = req.getParameter("tipFee");
		String productTotal = req.getParameter("productTotal");
		String payType = req.getParameter("payType");
			if("货到付款".equals(payType)) {
				payType="1";
			}else if("款到发货".equals(payType)) {
				payType="2";
			}else if("预付款到发货".equals(payType)) {
				payType="3";
			}
		String remark = req.getParameter("remark");
		String prePayFee = req.getParameter("prePayFee");
		String[] productCodes = req.getParameterValues("productCode");
		String[] name = req.getParameterValues("name");
		String[] unitName = req.getParameterValues("unitName");
		String[] num = req.getParameterValues("num");
		String[] unitPrice = req.getParameterValues("unitPrice");
		List<Poitem> poitems = new ArrayList<Poitem>();
		for(int i=0;i<productCodes.length;i++) {//遍历生成poitem对象集合
			Poitem poitem = new Poitem();
			poitem.setPoId(Long.parseLong(poId));
			poitem.setProductCode(productCodes[i]);
			poitem.setUnitName(unitName[i]);
			poitem.setUnitPrice(Double.parseDouble(unitPrice[i]));
			poitem.setNum(Integer.parseInt(num[i]));
			poitem.setItemPrice(Double.parseDouble(unitPrice[i])*Integer.parseInt(num[i]));
			poitems.add(poitem);
		}
		//生成pomain对象
		Pomain pomain = new Pomain();
		pomain.setPoId(Long.parseLong(poId));
		pomain.setCreateTime(createTime);
		pomain.setVenderCode(venderCode);
		pomain.setAccount(account);
		pomain.setTipFee(Double.parseDouble(tipFee));
		pomain.setPoTotal(Double.parseDouble(productTotal)+Double.parseDouble(tipFee));
		pomain.setProductTotal(Double.parseDouble(productTotal));
		pomain.setPayType(Integer.parseInt(payType));
		pomain.setRemark(remark);
		pomain.setPrePayFee(Double.parseDouble(prePayFee));
		pomain.setStatus(1);
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);//事务处理
			PoitemDao poitemdao = new PoitemDao(conn); 
			PomainDao pomaindao = new PomainDao(conn);
			
			//减少产品表在途数
			List<Poitem> delpoitems = new ArrayList<Poitem>();
			PoitemDao poitemdao2 = new PoitemDao();
			delpoitems = poitemdao2.selectByPoid(Long.parseLong(poId));
			int[] delnums = new int[delpoitems.size()];
			for(int n=0;n<delpoitems.size();n++) {
				String productCode = delpoitems.get(n).getProductCode();
				int number = -delpoitems.get(n).getNum();
				int delnum = new ProductDao(conn).changePONum(productCode, number);
				delnums[n]=delnum;
			}
			
			//删除明细表	
			int k = poitemdao.deleteByPoid(Long.parseLong(poId));
			
			//增加产品表在途数
			int addnums[] = new int[poitems.size()];
			for(int n=0;n<poitems.size();n++) {
				String productCode = poitems.get(n).getProductCode();
				int number = poitems.get(n).getNum();
				int addnum = new ProductDao(conn).changePONum(productCode, number);
				addnums[n]=addnum;
			}
			
			//增加明细表	
			int[] arr = poitemdao.add(poitems); 
			
			//修改采购表
			int i = pomaindao.change(pomain);
			
			//回滚事务
			if(i == 0){
				conn.rollback();
				return;
			}
			if(k == 0){
				conn.rollback();
				return;
			}
			for(int j:arr){
				if(j == 0){
					conn.rollback();
					return;
				}
			}
			for(int delnum:delnums){
				if(delnum == 0){
					conn.rollback();
					return;
				}
			}
			for(int addnum:addnums){
				if(addnum == 0){
					conn.rollback();
					return;
				}
			}
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			DbUtil.close(conn, null, null);
		}
		req.getRequestDispatcher("ListNewPomain").forward(req, resp);
	}

}
